package net.yla.smartbus.module.controller.web.ba.right.menu;

import javassist.bytecode.ByteArray;
import net.yla.smartbus.core.api.ApiResult;
import net.yla.smartbus.core.base.BaseController;
import net.yla.smartbus.core.constants.UserRoleEnum;
import net.yla.smartbus.core.util.DtoUtils;
import net.yla.smartbus.core.validation.DtoName;
import net.yla.smartbus.module.ba.right.menu.entity.MenusDO;
import net.yla.smartbus.module.ba.right.menu.query.*;
import net.yla.smartbus.module.ba.right.menu.service.MenusService;
import net.yla.smartbus.module.ba.right.roles.query.QueryOfView;
import net.yla.smartbus.module.ba.right.roles.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Date: 2020-08-19 16:56
 * @Author: Being Nobody
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/web/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenusService menusService;
    @Autowired
    private RolesService rolesService;

    /**
     * 1获取当前管理菜单
     *
     * @param query
     * @return
     */
    @PostMapping("/current")
    public ApiResult menu(@RequestBody @Validated CurrentOfQuery query) {
        // 获取所有菜单
        return success(menusService.menu(getToken(), query));
    }

    /**
     * 2获取指定父级的菜单记录数
     *
     * @param menusOfCount
     * @return
     */
    @PostMapping("/count")
    public ApiResult count(@RequestBody @Validated MenusOfCount menusOfCount) {
        return success(menusService.menusOfCount(menusOfCount));
    }

    /**
     * 3查询顶级菜单的记录集
     *
     * @param query
     * @return
     */
    @PostMapping("/list")
    public ApiResult list(@RequestBody @Validated MenusOfQuery query) {
        return success( menusService.list(query));
    }

    /**
     * 4 查看指定菜单的信息记录
     *
     * @param query 菜单记录的标识号
     * @return
     */
    @PostMapping("/view")
    public ApiResult view(@RequestBody QueryOfView query) {
        return success(menusService.view(query));
    }


    /**
     * 5添加或更新菜单
     *
     * @param save
     * @return
     */
    @PostMapping("/save")
    public ApiResult save(@RequestBody @Validated MenusOfSave save) {
        return success(menusService.save(save));
    }


    /**
     * 6删除菜单
     *
     * @param query
     * @return
     */
    @PostMapping("/del")
    public ApiResult del(@RequestBody @Validated QueryOfView query) {
        return success(menusService.del(query));
    }

    /**
     * test
     *
     * @param id
     * @return
     */
    @PostMapping("/select")
    public ApiResult save(Integer id) {
        MenusDO menusDO = menusService.selectByPrimaryKey(id);
        byte[] children = menusDO.getChildren();
        int i = ByteArray.read32bit(children, 0);
        return success(menusService.selectByPrimaryKey(id));
    }

    /**
     * 获取角色拥有的各个系统
     *
     * @param ids
     * @return
     */
    @PostMapping("/system")
    public ApiResult system(@RequestBody List<Integer> ids) {
        return success(menusService.menuValue(ids));
    }

    /**
     * 查询菜单
     * @param query
     * @return
     */
    @PostMapping("/page/query")
    public ApiResult listByQuery(@RequestBody TaxiMenuQuery query){
        rolesService.userRoleEvaluation(UserRoleEnum.USER_MENU_SELECT,getToken(),getIpAddress());
        return success(menusService.listByQuery(query));
    }


}
